package com.neteast.androidclient.contact.activity;

import com.neteast.androidclient.contact.domain.Contact;
import com.neteast.androidclient.contact.widget.InfoDetailPanel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class NewContactActivity extends Activity{
	private final int GET_PHOTO_FROM_GALLERY = 1;
	private final int TAKING_PICTURE = 2;
	private Context mContext;
	private boolean isNew;
	private Contact contact;
	private InfoDetailPanel infoDetail;
	private int position;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		isNew = getIntent().getBooleanExtra("isNew", false);
		contact = (Contact) getIntent().getSerializableExtra("contactInfo");
		position = getIntent().getIntExtra("position", -1);
		infoDetail=new InfoDetailPanel(NewContactActivity.this, contact);
		setContentView(infoDetail);
		init();
		
	}
	
	
	private void init() {
		WindowManager wManager = getWindowManager();
		Display display = wManager.getDefaultDisplay();
		LayoutParams lParams = getWindow().getAttributes();
		lParams.height = display.getHeight();
		lParams.width = 415;
		getWindow().setAttributes(lParams);
		getWindow().setGravity(Gravity.RIGHT);
		mContext = NewContactActivity.this;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(data == null){
			return;
		}
		contact = (Contact) data.getSerializableExtra("contact");
		refresh();
		super.onActivityResult(requestCode, resultCode, data);
	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(position != -1){
			setResult(Activity.RESULT_OK, new Intent(mContext, ContactActivity.class)
			.putExtra("position", position)
			.putExtra("contact", contact));
		}
		finish();
		return super.onTouchEvent(event);
	}

	private void refresh(){
		infoDetail=new InfoDetailPanel(this, contact);
		setContentView(infoDetail);
	}

}
